﻿using STHPMS1.BLL.B_Core;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.Common.C_Enum;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using STHPMS1.Common.C_Extension;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Serv;
using System.Linq;
using System.Data;

namespace STHPMS1.BLL.B_Bus
{
    public class Dr_Largemeas_IniBus : BllBase, IDr_Largemeas_IniBill
    {
        public void Save<T>(T entity)
            where T : class ,IDr_Largemeas_Ini
        {
            ///从路面结构中获取弯沉，从事故表中获取事故发生数量
            ISthpms1Dao dao=this.Scene.Dal.Sthpms1;
            Dn_Divi_Result mainSeg=dao.Single<Dn_Divi_Result>(p=>p.Mainsegid==entity.Mainsegid);
            if (mainSeg == null) throw new Exception("该路段数据不存在 -"+entity.Mainsegid);
            B_Pmstc pmstc = dao.Single<B_Pmstc>(p => p.Rdwayid == mainSeg.Rdwayid && mainSeg.Beginmp >= p.Beginmp && mainSeg.Endmp <= p.Endmp);
            if (pmstc == null) throw new Exception("该路段的路面结构数据不存在 -" + entity.Mainsegid);
            entity.Major_Accid =(int) dao.Count<S_Rdcar>(p => p.Rdwayid == mainSeg.Rdwayid && 
                                                          p.Carmp >= mainSeg.Beginmp && 
                                                          p.Carmp <= mainSeg.Endmp && 
                                                          p.Adtgd_Dictid < AccidentTypeEnum.Larger.GetEnumValue());

            entity.Degdf = pmstc.Degdf;
            entity.Design_Time = DateTime.Now;
            entity.Divi_Time = DateTime.Parse(mainSeg.Divi_Time);
            entity.Iniproj_Id = MyApp.Locator.GetInstance<IKeyGenrateService>().NewPlanId(mainSeg.Rdwayid,
                                                                                        mainSeg.Beginmp.ToString(),
                                                                                        mainSeg.Direction_Dictid.ToString(),
                                                                                        (DateTime)entity.Design_Time);
            
            this.Scene.Dal.Sthpms1.Save(entity);
        }

        public void Remove(IDr_Largemeas_Ini entity)
        {
            this.Scene.Dal.Sthpms1.DeleteById<Dr_Largemeas_Ini>(entity.Iniproj_Id.ToString());
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IDr_Largemeas_Ini
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info,p=>p.Design_Time,true);
        }

        public double  GetMillingThinkness(string mainSegId, int materialType)
        {
            Dn_Divi_Result segInfo = this.Scene.Dal.Sthpms1.Dn_Divi_Result.GetNewSegment<Dn_Divi_Result>(p => p.Mainsegid == mainSegId).FirstOrDefault();
            if(segInfo==null)throw new Exception("该路段不存在 - "+mainSegId);

            double value = 0;
            if(materialType==MaterialTypeEnum.M307.GetEnumValue())
            {
                value=this.Scene.Dal.Sthpms1.B_Pmstc.CalcMillingThinkness<B_Pmstc>(p => (p.Rdwayid == segInfo.Rdwayid
                    && segInfo.Beginmp >= p.Beginmp
                    && segInfo.Endmp <= p.Endmp
                    && segInfo.Direction_Dictid == p.Direction_Dictid)
                    //TODO:路面结构变动需要更新代码
                   // && p.Layer_Dictid == LayerEnum.Top.GetEnumValue() 
                    );
            }
            else if (materialType == MaterialTypeEnum.M308.GetEnumValue())
            {
                value = this.Scene.Dal.Sthpms1.B_Pmstc.CalcMillingThinkness<B_Pmstc>(p => (p.Rdwayid == segInfo.Rdwayid
                    && segInfo.Beginmp >= p.Beginmp
                    && segInfo.Endmp <= p.Endmp
                    && segInfo.Direction_Dictid == p.Direction_Dictid)
                    //TODO:路面结构变动需要更新代码
                    //&& (p.Layer_Dictid == LayerEnum.Top.GetEnumValue()
                    //    || p.Layer_Dictid==LayerEnum.Middle.GetEnumValue())
                    );
            }
            else if (materialType == MaterialTypeEnum.M309.GetEnumValue())
            {
                value = this.Scene.Dal.Sthpms1.B_Pmstc.CalcMillingThinkness<B_Pmstc>(p => (p.Rdwayid == segInfo.Rdwayid
                   && segInfo.Beginmp >= p.Beginmp
                   && segInfo.Endmp <= p.Endmp
                   && segInfo.Direction_Dictid == p.Direction_Dictid)
                   //TODO:路面结构变动需要更新代码
                    //&& (p.Layer_Dictid == LayerEnum.Top.GetEnumValue()
                       //|| p.Layer_Dictid == LayerEnum.Middle.GetEnumValue()
                       //|| p.Layer_Dictid == LayerEnum.Down.GetEnumValue())
                   );
            }
            return value;
        }

        public List<string> GetMainSeg()
        {
            return this.Scene.Dal.Sthpms1.Dr_Largemeas_Ini.GetMainSeg();
        }
    }
}
